<?php
include_once("dbconnect/db.php");
include_once("Common.php");
Class TopPage
{	
	var $html;
	var $topKeyword;
	var $topRank;
	var $catList;
	// 広告1
	var $advHtml1 = "";
	// 広告2
	var $advHtml2 = "";
	// 広告3
	var $advHtml3 = "";
	
	function doTask()
	{
		$this->getTopKeyword();
		$this->getTopRank();
		$this->getBigCatList();
	}
	
	function getTopKeyword()
	{
		$topkeyword = "";
		
		//where TO_DAYS(NOW()) - TO_DAYS(date) <= 30
		$sql = "select 	search_word 
		from search_history 
		where TO_DAYS(date) = TO_DAYS(NOW()) 
		order by search_cnt desc
		limit 0,5";
		
		
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		
		if($rows > 0)
		{
			$topkeyword = "本日のﾗﾝｷﾝｸﾞ:\n";
			while($row=mysql_fetch_array($result)) 
			{
				$decodekeyword=urlencode(mb_convert_encoding($row[0], 'SJIS', 'auto')) ;

				$topkeyword .= "<a href=\"result_list.php?keyword=$decodekeyword\" utn>$row[0]</a> \n" ; 
			}
		}
		
		$this->topKeyword = $topkeyword ;
	}
	
	function getTopRank()
	{
		$topRank = "";
		//$yesterday = date("Ymd",mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"))); 
		$sql = "
		SELECT vi.video_id, vi.cid, ent.title
		FROM videolist vi join entrylist ent on vi.entry_id=ent.entry_id
		WHERE vi.site_id = 1 and vi.cid is not null
		ORDER BY view_cnt_y desc
		limit 0,3 ";
 
		$result = mysql_query($sql);		
		$rows = mysql_num_rows($result);
		
		if($rows == 0) return;

		for ($i=1;$i<$rows+1;$i++)
		{
			$row = mysql_fetch_array($result);
			$link="result.php?type=1&videoId=$row[0]";
			$imagSrc=Common::getThumbByCID($row[1]);
			$topRank .= "<font color=\"#FF0066\">$i 位</font><br>";
			$topRank .= "<font size=\"1\"><a href=\"$link\"><img src=\"$imagSrc\" align=\"left\">$row[2]</a><br clear=\"all\"></font>\n" ;
			

		}

		$this->topRank = $topRank ;
	}
	
	function getBigCatList()
	{
		$html = "";
		$sql = "SELECT category_id,category_name 
				FROM   category_mst 
				WHERE  pid = 0 and del_flag=0
				ORDER BY category_id
				limit 0,3";
		$result = mysql_query($sql);
		while($row=mysql_fetch_array($result)) 
		{
       		$html.='<a href="result_list.php?categoryId='.$row[0].'" utn>'.$row[1].'</a><br>'."\n";
   		}
		
		$this->catList = $html;
	}
	
	function getAdvHtml()
	{
		$sql = $this->getAdvSql("1");
		$resultAd = mysql_query($sql);
		$rowsAd = mysql_num_rows($resultAd);
		
		// 広告が存在の場合
		if($rowsAd > 0)
		{
			$data = mysql_fetch_array($resultAd);
			$this->advHtml1 = $this->advHtml1."<hr color=#3399FF>".$data["ad_name"]."<font size=1>";
			$this->advHtml1 = $this->advHtml1.$data["tag_contents"]."<br clear=all></font>";
		}	
		
		$sql = $this->getAdvSql("2");
		
		$resultAd = mysql_query($sql);
		$rowsAd = mysql_num_rows($resultAd);
		
		// 広告が存在の場合
		if($rowsAd > 0)
		{
			$data = mysql_fetch_array($resultAd);
			$this->advHtml2 = $this->advHtml2."<hr color=#3399FF>".$data["ad_name"]."<font size=1>";
			$this->advHtml2 = $this->advHtml2.$data["tag_contents"]."<br clear=all></font>";
		}
		
		$sql = $this->getAdvSql("3");
		
		$resultAd = mysql_query($sql);
		$rowsAd = mysql_num_rows($resultAd);
		
		// 広告が存在の場合
		if($rowsAd > 0)
		{
			$data = mysql_fetch_array($resultAd);
			$this->advHtml3 = $this->advHtml3."<hr color=#3399FF>".$data["ad_name"]."<font size=1>";
			$this->advHtml3 = $this->advHtml3.$data["tag_contents"]."<br clear=all></font>";
		}		
			
	}
	
	function getAdvSql($id)
	{
		$sql = "select adspace_id,ad_name,tag_contents 
		       from adspace_mst 
			   where type = 2 and open_flag = 1 and adspace_id = ".$id;
		 
		return $sql;
	}

}
?>